package com.qys.livesMall.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.qys.livesMall.common.constants.Constants;
import com.qys.livesMall.common.exception.QysException;
import com.qys.livesMall.common.exception.enums.ExceptionEnum;
import com.qys.livesMall.shop.enums.ShopMessageEnum;
import com.qys.livesMall.shop.modal.entity.ShopRechargeGradient;
import com.qys.livesMall.shop.mapper.ShopRechargeGradientMapper;
import com.qys.livesMall.shop.modal.vo.ShopRechargeGradientVO;
import com.qys.livesMall.shop.service.ShopRechargeGradientService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 店铺充值返现梯度 服务实现类
 * </p>
 *
 * @author
 * @since 2023-10-24
 */
@Service
public class ShopRechargeGradientServiceImpl extends ServiceImpl<ShopRechargeGradientMapper, ShopRechargeGradient> implements ShopRechargeGradientService {


    public ShopRechargeGradient getByPoint(BigDecimal point){
        LambdaUpdateWrapper<ShopRechargeGradient> wq=new LambdaUpdateWrapper<>();
        wq.eq(ShopRechargeGradient::getSplitPoint,point);
        return getOne(wq);
    }
    @Override
    public void addOrUp(ShopRechargeGradient gradient) {
        if (gradient==null||gradient.getSplitPoint()==null||gradient.getAmount()==null){
            throw new QysException(ExceptionEnum.PARAM_IS_ERROR);
        }
        //查询分割点是否存在
        ShopRechargeGradient temp=getByPoint(gradient.getSplitPoint());
        if (gradient.getId()==null){
            //新增
            if(temp!=null){
                throw new QysException(ShopMessageEnum.POINT_IS_EXIST);
            }
        }else {
            temp.setAmount(gradient.getAmount());
            gradient=temp;
        }
        gradient.setStatus(Constants.YesNoInt.YES);
        saveOrUpdate(gradient);
    }

    @Override
    public void upStatus(ShopRechargeGradient gradient) {
        if (gradient==null||gradient.getStatus()==null){
            throw new QysException(ExceptionEnum.PARAM_IS_ERROR);
        }
        //查询分割点是否存在
        ShopRechargeGradient oldGradient=getByPoint(gradient.getSplitPoint());
        if(oldGradient==null){
            throw new QysException(ShopMessageEnum.POINT_IS_NOT_EXIST);
        }
        oldGradient.setStatus(gradient.getStatus());
        updateById(oldGradient);
    }

    @Override
    public List<ShopRechargeGradientVO> getAll() {
        LambdaUpdateWrapper<ShopRechargeGradient> wq=new LambdaUpdateWrapper<>();
        wq.orderByAsc(ShopRechargeGradient::getSplitPoint);
        List<ShopRechargeGradient> list= list(wq);
        if (CollectionUtils.isEmpty(list)){
            return null;
        }
        List<ShopRechargeGradientVO> vos=new ArrayList<>();
        for(int i=0;i<list.size();i++){
            ShopRechargeGradientVO vo=new ShopRechargeGradientVO();
            vo.setId(list.get(i).getId());
            vo.setAmount(list.get(i).getAmount());
            vo.setStatus(list.get(i).getStatus());
            vo.setSplitPoint(list.get(i).getSplitPoint());
            vo.setMinPoint(list.get(i).getSplitPoint());
            if (i+1<list.size()){
                vo.setMaxPoint(list.get(i+1).getSplitPoint());
            }
            vos.add(vo);
        }
        return vos;
    }

    @Override
    public ShopRechargeGradient getByAmount(BigDecimal amount) {
        LambdaUpdateWrapper<ShopRechargeGradient> wq=new LambdaUpdateWrapper<>();
        wq.ge(ShopRechargeGradient::getSplitPoint,amount);
        wq.orderByAsc(ShopRechargeGradient::getSplitPoint);
        wq.last("limit 1");
        return getOne(wq);
    }
}
